import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '@/layout/index'

Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = (location) => originalPush.call(this, location).catch(err => err)

const constRouterMap = [
    {
        path: '/login',
        name: 'login',
        meta: { title: '登录' },
        component: (resolve) => require(['@/views/login'], resolve)
    },
    {
        path: '/redirect',
        component: Layout,
        hidden: true,
        children: [
            {
                path: '/redirect/:path*',
                component: (resolve) => require(['@/views/redirect'], resolve)
            }
        ]
    },
    {
        path: '/',
        component: Layout,
        redirect: '/index',
        children: [
            {
                path: '/index',
                name: 'index',
                component: (resolve) => require(['@/views/home'], resolve),
                meta: { title: '首页', icon: 'index', affix: true, noCache: true }
            }
        ]
    },
    {
        path: '/404',
        name: 'noFound',
        component: resolve => require(['@/views/404'], resolve),
        meta: { title: '404' }
    }
]

export default new VueRouter({
    mode: 'history',
    scrollBehavior: () => ({y: 0}),
    routes: constRouterMap
})